package org.opu.translator.gui;

import java.awt.Color;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 *
 * @author yura
 */
public class VVTable extends JTable {
    private String head[];

    public VVTable() {
        super(new DefaultTableModel(null, new String[]{"Выражение", "Значение"}));
        head = new String[2];
        head[0] = "Регулярное выражение";
        head[1] = "Значение из текста";
        setDragEnabled(false);
        setBackground(Color.WHITE);
        getTableHeader().setReorderingAllowed(false);
        setRowSelectionAllowed(true);
        setFillsViewportHeight(true);
    }

    public void removeAllRow (){
        int rowCount = getRowCount();
        for (int i = 0; i < rowCount; i++) {
            removeRow(0);
        }
    }

    public void removeRow(int row) {
        ((DefaultTableModel)getModel()).removeRow(row);
    }

    public void addRow(String var, Value val) {
        ((DefaultTableModel)getModel()).addRow(new Object[]{var, val});
    }

    @Override
    public String getToolTipText(MouseEvent e) {
        int c = columnAtPoint(e.getPoint());
        int r = rowAtPoint(e.getPoint());
        if (c < 0 || r < 0) {
            return getToolTipText();
        }
        try {
            return String.valueOf(getValueAt(r, c));
        } catch (Exception ex) {
            return getToolTipText();
        }
    }

    @Override
    protected JTableHeader createDefaultTableHeader() {
        return new JTableHeader(columnModel) {

            @Override
            public String getToolTipText(MouseEvent e) {
                int index = columnModel.getColumnIndexAtX(e.getPoint().x);
                int realIndex = columnModel.getColumn(index).getModelIndex();
                if (head.length > realIndex) {
                    return head[realIndex];
                } else {
                    return getToolTipText();
                }
            }
        };
    }

}
